package com.trade.wzj.common.xiang;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.trade.wzj.model.xiang.ClientInfo;
import com.trade.wzj.model.xiang.ClientOrderInfo;
import com.trade.wzj.model.xiang.ClientOrderinfoDetails;
import com.trade.wzj.model.xiang.ClientQuota;
import com.trade.wzj.model.xiang.ClientQuotaDetails;
import com.trade.wzj.model.xiang.CompanyPurchaseDetails;
import com.trade.wzj.model.xiang.CompanyPurchaseInfo;
import com.trade.wzj.model.xiang.ProductInfoQuota;
import com.trade.wzj.model.xiang.SupplierInfo;
import com.trade.wzj.model.xiang.SupplierQuota;
import com.trade.wzj.service.xiang.ClientAddService;
import com.trade.wzj.service.xiang.ClientOrderInfoService;
import com.trade.wzj.service.xiang.ClientOrderinfoDetailsService;
import com.trade.wzj.service.xiang.ClientQuotaDetailsService;
import com.trade.wzj.service.xiang.ClientQuotaService;
import com.trade.wzj.service.xiang.ProductInfoQuotaService;
import com.trade.wzj.service.xiang.SupplierInfoService;
import com.trade.wzj.service.xiang.SupplierQuotaService;

@Controller
@RequestMapping("/tests")
public class ClientContriller {
	private Logger logger = Logger.getLogger(ClientContriller.class);
	@Resource(name = "ClientAdd")
	private ClientAddService clientaddservice;
	@Resource(name = "SupplierInfoAdd")
	private SupplierInfoService supplierinfoservice;
	@Resource(name = "SupplierQuotaAdd")
	private SupplierQuotaService supplierquotaservice;
	@Resource(name = "ProductInfoQuotaAdd")
	private ProductInfoQuotaService productinfoquotaservice;
	@Resource(name = "ClientQuotaAdd")
	private ClientQuotaService clientquotaservice;
	@Resource(name = "ClientQuotaDetailsAdd")
	private ClientQuotaDetailsService clientquotadetailsservice;
	@Resource(name = "ClientOrderInfoAdd")
	private ClientOrderInfoService clientorderinfoservice;
	@Resource(name = "ClientOrderinfoDetailsAdd")
	private ClientOrderinfoDetailsService clientorderinfodetailsservice;

	public ClientQuotaDetailsService getClientquotadetailsservice() {
		return clientquotadetailsservice;
	}

	public void setClientquotadetailsservice(ClientQuotaDetailsService clientquotadetailsservice) {
		this.clientquotadetailsservice = clientquotadetailsservice;
	}

	public ClientQuotaService getClientquotaservice() {
		return clientquotaservice;
	}

	public void setClientquotaservice(ClientQuotaService clientquotaservice) {
		this.clientquotaservice = clientquotaservice;
	}

	public ProductInfoQuotaService getProductinfoquotaservice() {
		return productinfoquotaservice;
	}

	public void setProductinfoquotaservice(ProductInfoQuotaService productinfoquotaservice) {
		this.productinfoquotaservice = productinfoquotaservice;
	}

	public SupplierQuotaService getSupplierquotaservice() {
		return supplierquotaservice;
	}

	public void setSupplierquotaservice(SupplierQuotaService supplierquotaservice) {
		this.supplierquotaservice = supplierquotaservice;
	}

	public SupplierInfoService getSupplierinfoservice() {
		return supplierinfoservice;
	}

	public void setSupplierinfoservice(SupplierInfoService supplierinfoservice) {
		this.supplierinfoservice = supplierinfoservice;
	}

	public ClientAddService getClientaddservice() {
		return clientaddservice;
	}

	public void setClientaddservice(ClientAddService clientaddservice) {
		this.clientaddservice = clientaddservice;
	}

	// 客户信息添加
	@RequestMapping("/kfAdd")
	@ResponseBody
	public boolean Client(ClientInfo info) {
		boolean flag = true;
		try {
			flag = clientaddservice.AddClient(info);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	// 供应商信息添加
	@RequestMapping("/gysAdd")
	@ResponseBody
	public boolean Supplier(SupplierInfo supplierinfo) {
		boolean id = false;
		try {
			id = supplierinfoservice.SupplierInfoAdd(supplierinfo);
			id = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return id;
	}

	// 供应商报价添加
	@RequestMapping("/gyslbAdd")
	@ResponseBody
	public boolean gyslbAdd(SupplierQuota sq) {
		boolean rs = true;
		try {
			rs = supplierquotaservice.SupplierQuotaAdd(sq);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}

	// 供应商报价删除
	@RequestMapping("/gyslbDel")
	public boolean gyslbDel(int id) {
		boolean de = true;
		try {
			de = supplierquotaservice.SupplierQuotaDel(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return de;
	}

	// 客户报价添加
	@RequestMapping("/kfbjAdd")
	@ResponseBody
	public int cpbjAdd(@RequestParam(value="clientId",required=false)Integer clientId, @RequestParam(value="clientquotadetails[]",required=false)ClientQuotaDetails[] clientquotadetails,
			@RequestParam(value="productInfoQuotaID",required=false)List<Integer> productInfoQuotaID,
			@RequestParam(value="productNum",required=false)List<Integer> productNum,
			@RequestParam(value="quotaPrice",required=false)List<Double> quotaPrice,
			@RequestParam(value="expirationDate",required=false)List<String> expirationDate,
			@RequestParam(value="quotaMoney",required=false)List<Double> quotaMoney,HttpServletRequest request) {
		
		//String[] productInfoQuotaIDs = request.getParameterValues("productInfoQuotaID");
		
		int result = 0;
		List<ClientQuotaDetails> productList=new ArrayList<ClientQuotaDetails>();
		try {
			
			
			ClientQuota cli = new ClientQuota();
			
			cli.setClientInfoID(clientId);
			cli.setPayID(1);
			//供应商产品ID查询
			ProductInfoQuota productInfo=null;
			
				//Integer par =Integer.parseInt(productInfoQuotaID);
				//productInfo= productinfoquotaservice.ProductInfoQuotaSel(par);
			//查询客户报价编号
			ClientQuota results = clientquotaservice.findClientId(clientId);
			//客户报价编号
			int clientPriceId=0;
				//客户报价编号
		if(results==null) {
			//生成客户报价编号
			clientPriceId = clientquotaservice.ClientQuotaAdd(cli);
		
				String clientQuota = String.valueOf(clientPriceId);
				
				for(int i=0;i<productInfoQuotaID.size();i++) {
					ClientQuotaDetails cl = new ClientQuotaDetails();
					cl.setProductID(productInfoQuotaID.get(i));
					cl.setProductNum(productNum.get(i));
					cl.setQuotaPrice(quotaPrice.get(i));
					cl.setClientQuotaID(cli.getId());
					cl.setQuotaMoney(quotaMoney.get(i)); 
					cl.setClientQuotaDetailsID(clientQuota);
					cl.setExpirationDates(expirationDate.get(i));
					productList.add(cl);
				}
		}else {
			//clientPriceId = results;
			String clientQuota = String.valueOf(results.getClientQuotaID());
			
			for(int i=0;i<productInfoQuotaID.size();i++) {
				ClientQuotaDetails cl = new ClientQuotaDetails();
				cl.setProductID(productInfoQuotaID.get(i));
				cl.setProductNum(productNum.get(i));
				cl.setQuotaPrice(quotaPrice.get(i));
				cl.setClientQuotaID(results.getId());
				cl.setQuotaMoney(quotaMoney.get(i)); 
				cl.setClientQuotaDetailsID(clientQuota);
				cl.setExpirationDates(expirationDate.get(i));
				productList.add(cl);
			}
		}
				result = clientquotadetailsservice.ClientQuotaDetailsAdd(productList);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	// 供应商产品添加
	@RequestMapping("/gyscpAdd")
	public boolean gyscpAdd(ProductInfoQuota productinfoquota) {
		boolean result = true;
		try {
			result = productinfoquotaservice.ProductInfoQuotaAdd(productinfoquota);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	// 供应商产品删除
	@RequestMapping("gyscpDel")
	public boolean gyscpDel(int productInfoQuotaID) {
		boolean de = true;
		try {
			de = productinfoquotaservice.ProductInfoQuotaDel(productInfoQuotaID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return de;
	}

	// 客户报价详情添加
	// @RequestMapping("/kfxqAdd")
	// public String cpxqAdd(Integer productInfoQuotaID,ClientQuotaDetails
	// clientquotadetails) {
	// try {
	// //供应商产品ID查询
	// List<ProductInfoQuota> pc =
	// productinfoquotaservice.ProductInfoQuotaSel(productInfoQuotaID);
	// //客户报价单获取客户编号
	// ClientQuota cd = new ClientQuota();
	//
	// for (ProductInfoQuota productInfoQuota : pc) {
	//
	// }
	// //客户报价单详情插入
	// int result=0;
	// result=clientquotadetailsservice.ClientQuotaDetailsAdd(clientquotadetails);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// return "xiangtest";
	// }

	// 客户订单添加
	@RequestMapping("/kfddAdd")
	@ResponseBody
	public boolean kfddAdd(@RequestParam(value="clientQuotaId",required=false)Integer clientQuotaId,
							@RequestParam(value="productID",required=false)List<Integer> productID,
							@RequestParam(value="productNum",required=false)List<Integer> productNum,
							@RequestParam(value="Unit",required=false)List<String> Unit,
							@RequestParam(value="quotaMoney",required=false)List<Double> quotaMoney,
							@RequestParam(value="deliveryDate",required=false)String deliveryDate,
							@RequestParam(value="quotaPrice",required=false)List<Double> quotaPrice) {
		ClientOrderInfo clientorderinfo = new ClientOrderInfo();
		//生成客户订单编号
		Integer ic = (int) (Math.random() * 9000) + 1000;
		//获取总金额
		Double totalMoney=0.0;
		for(int i=0;i<quotaMoney.size();i++) {
			totalMoney = totalMoney+quotaMoney.get(i);
		}
		String clientQuota = String.valueOf(ic);
		boolean result = false;
		clientorderinfo.setClientQuotaDetailsID(clientQuotaId);
		clientorderinfo.setClientOrderNo(clientQuota);
		clientorderinfo.setDeliveryDates(deliveryDate);
		clientorderinfo.setTotalMoney(totalMoney);
		//设置添加集合
		List<ClientOrderinfoDetails> list=new ArrayList<ClientOrderinfoDetails>();
		try {
			result = clientorderinfoservice.ClientOrderInfoAdd(clientorderinfo);
			//获取订单编号自增因子
			Integer id=clientorderinfo.getId();
			for (int i = 0; i < productID.size(); i++) {
				ClientOrderinfoDetails c = new ClientOrderinfoDetails();
				c.setClientQuotaID(id);
				c.setRental(quotaMoney.get(i));
				c.setStateID(1);
				c.setProductID(productID.get(i));
				c.setProductNum(productNum.get(i));
				c.setUnit(Unit.get(i));
				c.setOrderPrice(quotaPrice.get(i));
				list.add(c);
			}
			//调用订单详情添加方法
			int detailsAdd = clientorderinfodetailsservice.ClientOrderinfoDetailsAdd(list);
			if(detailsAdd!=0) {
				result=true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	// 客户订单详情添加
	@RequestMapping("/kfddanxqAdd")
	public int kfddanxqAdd(List<ClientOrderinfoDetails> clientorderinfodetails) {
		int resilt = 0;
		try {
			resilt = clientorderinfodetailsservice.ClientOrderinfoDetailsAdd(clientorderinfodetails);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resilt;
	}
	
	//查询客户订单信息
	@RequestMapping("/getAllClientOrderInfos")
	@ResponseBody
	public PageInfo<ClientOrderInfo> getAllClientOrderInfos(@RequestParam(value="pages",required=false)Integer pages,@RequestParam(value="clientName",required=false)String clientName){
		//分页
		PageInfo<ClientOrderInfo> pageInfo=null;
		Page<ClientOrderInfo> page =null;
		//页码
		Integer pageNum=1;
		if(null!=pages) {
			pageNum = pages;
		}
		//每页大小
		int pageSize=5;
		List<ClientOrderInfo> list=null;
		try {
			page = PageHelper.startPage(pageNum, pageSize,true);
			list=clientorderinfoservice.getAllClientOrderInfo(clientName);
			pageInfo = new PageInfo<ClientOrderInfo>(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageInfo;
	}
	
	//查询客户订单详情
	@RequestMapping("/findClientOrderDetailById")
	@ResponseBody
	public PageInfo<ClientOrderinfoDetails> findClientOrderDetailById(@RequestParam(value="clientOrderId",required=false)Integer clientOrderId,@RequestParam(value="pages",required=false)Integer pages,@RequestParam(value="productName",required=false)String productName){
		//分页
		PageInfo<ClientOrderinfoDetails> pageInfo=null;
		Page<ClientOrderinfoDetails> page =null;
		//页码
		Integer pageNum=1;
		if(null!=pages) {
			pageNum = pages;
		}
		//每页大小
		int pageSize=5;
		List<ClientOrderinfoDetails> list=null;
		try {
			page = PageHelper.startPage(pageNum, pageSize,true);
			list=clientorderinfodetailsservice.getClientOrderDetail(clientOrderId,productName);
			pageInfo = new PageInfo<ClientOrderinfoDetails>(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageInfo;
	}
	
	//获取公司采购信息
	@RequestMapping("/getCompanyOrderMethod")
	@ResponseBody
	public List<ClientOrderinfoDetails> getCompanyOrderMethod(@RequestParam(value="clientQuotaID",required=false)Integer[] clientQuotaID){
		List<ClientOrderinfoDetails> list=null;
		try {
			list=clientorderinfodetailsservice.getCompanyOrderMethod(clientQuotaID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	//添加公司采购单
	@RequestMapping("/addCompanyPurchase")
	@ResponseBody
	public boolean addCompanyPurchase(@RequestParam("purchaseDate")String purchaseDate,
									  @RequestParam("purchaseDelivery")String purchaseDelivery,
									  @RequestParam("clientOrderId")List<Integer> clientOrderId,
									  @RequestParam("productInfoQuotaID")List<Integer> productInfoQuotaID,
									  @RequestParam("unit")List<String> unit,
									  @RequestParam("supplierInfoID")List<Integer> supplierInfoID,
									  @RequestParam("productNum")List<Integer> productNum,
									  @RequestParam("productPrice")List<Double> productPrice,
									  @RequestParam("totalPrice")List<Double> totalPrice) {
		
		
		boolean bol=false;
		try {
		//公司采购单详情对象集合
		List<CompanyPurchaseDetails> list=new ArrayList<CompanyPurchaseDetails>();
				//获取采购单编号随机数
		for(int k=0;k<clientOrderId.size();k++) {
					
			CompanyPurchaseDetails con=new CompanyPurchaseDetails();
			//con.setCompanyPurchaseInfoID(companyPurchaseInfo.getId());
			con.setClientOrderId(clientOrderId.get(k));
			con.setRental(totalPrice.get(k));
			con.setProductNum(productNum.get(k));
			con.setDetailsupplierId(supplierInfoID.get(k));
			con.setUnit(unit.get(k));
			con.setStateID(1);
			con.setProductId(productInfoQuotaID.get(k));
			list.add(con);
							}
		//创建公司采购单对象
		CompanyPurchaseInfo companyPurchaseInfo=new CompanyPurchaseInfo();
		//companyPurchaseInfo.setPurchaseNo(companyPurchase);
		companyPurchaseInfo.setPurchaseDateStr(purchaseDate);
		companyPurchaseInfo.setPurchaseDeliveryStr(purchaseDelivery);
		//第一步循环得出供应商基本信息id，并保存起来
		List<Integer> supplierId=new ArrayList<Integer>();
		//区分产品编号
		List<Integer> proNo=new ArrayList<Integer>();
		for(int i=0;i<supplierInfoID.size();i++) {
			if(!supplierId.contains(supplierInfoID.get(i))) {
				supplierId.add(supplierInfoID.get(i));
			}
			//循环添加产品编号，相同的则不添加
			if(!proNo.contains(productInfoQuotaID.get(i))) {
				proNo.add(productInfoQuotaID.get(i));
			}
		}
		
		
		//第二步，通过循环当前供应商基本信息集合
		for(int i=0;i<supplierId.size();i++) {
			//获取随机数，生成公司采购单编号
			Integer ic = (int) (Math.random() * 9000) + 1000;
			String companyPurchase = String.valueOf(ic);
			companyPurchaseInfo.setPurchaseNo(companyPurchase);
			//订单总额
			Double total=0.0;
			//产品数量
			int productNums=0;
			//获取对应采购单总额
			//创建公司详情集合
			List<CompanyPurchaseDetails> conDetail=new ArrayList<CompanyPurchaseDetails>();
			for(int j=0;j<list.size();j++) {
				//判断与供应商相关的产品
				if(supplierId.get(i)==list.get(j).getDetailsupplierId()) {
					total = total+list.get(j).getRental();
					//如果该供应商存在有该产品编号
					if(proNo.contains(list.get(j).getProductId())) {
						//则添加该产品数量
						productNums=productNums+list.get(j).getProductNum();
					}
					companyPurchaseInfo.setSupplierId(supplierId.get(i));
					//list.get(j).setCompanyPurchaseInfoID(companyPurchaseInfo.getId());
					conDetail.add(list.get(j));
				}
			}
			//设置订单总额
			companyPurchaseInfo.setOrderTotal(total);
			
			//调用添加公司采购方法
			clientorderinfodetailsservice.addCompanyPurchaseInfo(companyPurchaseInfo);
			for(int u=0;u<conDetail.size();u++) {
				conDetail.get(u).setCompanyPurchaseInfoID(companyPurchaseInfo.getId());
			}
			
			//调用添加公司采购详情方法
			clientorderinfodetailsservice.addCompanyPurchaseDetail(conDetail);
			bol=true;
		}
		
		} catch (Exception e) {
			e.printStackTrace();		
		}
		return bol;
}
	
	@RequestMapping("/getAllCompanyPurchaseInfo")
	@ResponseBody
	public PageInfo<CompanyPurchaseInfo> getAllCompanyPurchaseInfo(@RequestParam(value="pages",required=false)Integer pages,@RequestParam(value="purchaseNo",required=false)String purchaseNo){
		//分页
		PageInfo<CompanyPurchaseInfo> pageInfo=null;
		Page<CompanyPurchaseInfo> page =null;
		//页码
		Integer pageNum=1;
		if(null!=pages) {
			pageNum = pages;
		}
		//每页大小
		int pageSize=5;
		List<CompanyPurchaseInfo> list=null;
		try {
			page=PageHelper.startPage(pageNum, pageSize,true);
			list=clientorderinfodetailsservice.getAllCompanyPurchaseInfo(purchaseNo);
			pageInfo=new PageInfo<CompanyPurchaseInfo>(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageInfo;
	}
	
	//查询公司采购详情
	@RequestMapping("/getAllCompanyPurchaseInfos")
	@ResponseBody
	public PageInfo<CompanyPurchaseDetails> getAllCompanyPurchaseInfos(@RequestParam(value="companyPurchaseInfoID",required=false)Integer companyPurchaseInfoID,
																  @RequestParam(value="DetailsupplierId",required=false)Integer DetailsupplierId,
																  @RequestParam(value="productName",required=false)String productName,
																  @RequestParam(value="pages",required=false)Integer pages){
		List<CompanyPurchaseDetails> list=null;
		//分页
		PageInfo<CompanyPurchaseDetails> pageInfo=null;
		Page<CompanyPurchaseDetails> page =null;
		//页码
		Integer pageNum=1;
		if(null!=pages) {
			pageNum = pages;
		}
		//每页大小
		int pageSize=5;
		try {
			page=PageHelper.startPage(pageNum, pageSize,true);
			list=clientorderinfodetailsservice.getAllCompanyPurchaseInfos(companyPurchaseInfoID, DetailsupplierId, productName);
			pageInfo = new PageInfo<CompanyPurchaseDetails>(page);
		} catch (Exception e) {
			e.printStackTrace();
		
	}
		return pageInfo;
}
	
	//通过采购id和供应商id查询采购编号和采购日期
	@RequestMapping("/getCompanyPurchaseNo")
	@ResponseBody
	public CompanyPurchaseInfo getCompanyPurchaseNo(@RequestParam(value="id",required=false)Integer id,
													@RequestParam(value="supplierId",required=false)Integer supplierId) {
		CompanyPurchaseInfo companyPurchaseInfo=null;
		try {
			companyPurchaseInfo=clientorderinfodetailsservice.getCompanyPurchaseId(id, supplierId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return companyPurchaseInfo;
	}
}
